package live.yanxiaohui.springboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Description todo
 * @CSDN https://blog.csdn.net/yxh13521338301
 * @Author： yanxh<br>
 * @Date 2020-06-28 17:21<br>
 * @Version 1.0<br>
 */
@Configuration
public class RabbitConfig {

    @Value("${mq.rabbit.exchange.fanout.name}")
    private String mqRabbitExchangeFanoutName;

    @Value("${mq.rabbit.exchange.topic.name}")
    private String mqRabbitExchangeTopicName;

    @Value("${mq.rabbit.exchange.direct.name}")
    private String mqRabbitExchangeDirectName;

    @Value("${mq.rabbit.queue.email.name}")
    private String mqRabbitQueueEmailName;

    @Value("${mq.rabbit.queue.sms.name}")
    private String mqRabbitQueueSmsName;

    /**
     * 短信队列
     * @return
     */
    @Bean
    public Queue smsQueue(){
        return new Queue(mqRabbitQueueSmsName);
    }

    /**
     * 邮件队列
     * @return
     */
    @Bean
    public Queue emailQueue(){
        return new Queue(mqRabbitQueueEmailName);
    }

    /**
     * 扇形交换机（发布订阅）
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(mqRabbitExchangeFanoutName);
    }


    /**
     * 短信队列绑定扇形交换机
     * @param smsQueue
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding smsBindingFanoutExchange(Queue smsQueue, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(smsQueue).to(fanoutExchange);
    }

    /**
     * 邮件队列绑定扇形交换机
     * @param emailQueue
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding emailBindingFanoutExchange(Queue emailQueue, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(emailQueue).to(fanoutExchange);
    }


    /**
     * 直连交换机（路由）
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(mqRabbitExchangeDirectName);
    }

    /**
     * 短信队列绑定直连交换机
     * @param smsQueue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding smsBindingDirectExchange(Queue smsQueue, DirectExchange directExchange){
        return BindingBuilder.bind(smsQueue).to(directExchange).with("yxh.#");
    }

    /**
     * 邮件队列绑定直连交换机
     * @param emailQueue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding emailBindingDirectExchange(Queue emailQueue, DirectExchange directExchange){
        return BindingBuilder.bind(emailQueue).to(directExchange).with("yxh.*");
    }

    /**
     * 主题交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(mqRabbitExchangeTopicName);
    }

    /**
     * 短信队列绑定主题交换机
     * @param smsQueue
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding smsBindingTopicExchange(Queue smsQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(smsQueue).to(topicExchange).with("yxh.#");
    }

    /**
     * 邮件队列绑定主题交换机
     * @param emailQueue
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding emailBindingTopicExchange(Queue emailQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(emailQueue).to(topicExchange).with("yxh.*");
    }
}
